package com.cn.wanxi.servlet.front;

import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.FacebookModel;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.service.ICompanyService;
import com.cn.wanxi.service.IFacebookService;
import com.cn.wanxi.service.INavService;
import com.cn.wanxi.service.impl.CompanyServiceImpl;
import com.cn.wanxi.service.impl.FacebookServiceImpl;
import com.cn.wanxi.service.impl.NavServiceImpl;
import com.cn.wanxi.util.DataEncoding;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/facebook")
public class FacebookServlet extends HttpServlet {
    ICompanyService companyService = new CompanyServiceImpl();
    INavService navService = new NavServiceImpl();
    IFacebookService facebookService = new FacebookServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CompanyModel companyModel = companyService.getCompany();
        List<NavModel> navList = navService.getNavList();
        System.out.println(companyModel.toString());
        req.setAttribute("company", companyModel);
        req.setAttribute("nav",navList);
        req.getRequestDispatcher("/jsp/facebook.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DataEncoding.setCharacterEncoding(req,resp);
        String title = req.getParameter("title");
        String email = req.getParameter("email");
        String content = req.getParameter("content");
        FacebookModel facebookModel = new FacebookModel();
        facebookModel.setTitle(title);
        facebookModel.setEmail(email);
        facebookModel.setContent(content);
        facebookModel.setCreateTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date().getTime()));
        int result = facebookService.addFacebook(facebookModel);
        if (result==1)
            req.getRequestDispatcher("/jsp/success.jsp").forward(req,resp);
        else
            resp.sendRedirect("/jsp/facebook.jsp");
    }
}
